package com.hktf.moedokilock.widget;

import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.moedokilock.R;
import com.hktf.moedokilock.adapter.DataFormatItemAdapter;
import com.hktf.moedokilock.adapter.GridSpacingItemDecoration;
import com.hktf.moedokilock.adapter.WidgetItemAdapter;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.bean.KeyValueItem;
import com.hktf.uilib.bean.lock.DateFormatItem;
import com.hktf.uilib.bean.lock.WidgetItem;
import com.hktf.uilib.utils.DlgUtils;

import java.util.ArrayList;
import java.util.List;

public class PickWidgetPopWindow extends PopupWindow {
    private RecyclerView recyclerView;

    private WidgetItemAdapter widgetItemAdapter;


    public PickWidgetPopWindow(final BaseActivity context, MyListener listener) {
        this.myListener = listener;
        View rootView = View.inflate(context, R.layout.pick_widget_pop, null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView = rootView.findViewById(R.id.recyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(context, Config.WIDGET_SPACE);
        recyclerView.setLayoutManager(layoutManager);


        rootView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

//        boolean isCheck, int itemType, int iconResId, int weight, String title
        List<WidgetItem> list = new ArrayList<>();
        list.add(new WidgetItem(false, WidgetItem.TYPE_TITLE, R.drawable.wenben, CommUtils.getString(com.hktf.uilib.R.string.widget_text_title)));
        list.add(new WidgetItem(false, WidgetItem.TYPE_JITANG, -1, CommUtils.getString(com.hktf.uilib.R.string.widget_text_dujitang)));
        list.add(new WidgetItem(false, WidgetItem.TYPE_TEXT2, -1, CommUtils.getString(com.hktf.uilib.R.string.widget_text_input)));
        list.add(new WidgetItem(false, WidgetItem.TYPE_TEXT1, -1, CommUtils.getString(com.hktf.uilib.R.string.widget_text_input)));
        list.add(new WidgetItem(false, WidgetItem.TYPE_TEXT_CIRCLE, -1, CommUtils.getString(com.hktf.uilib.R.string.widget_text_circle)));

        list.add(new WidgetItem(false, WidgetItem.TYPE_TITLE, R.drawable.jieya, CommUtils.getString(com.hktf.uilib.R.string.widget_gif_title)));
        list.add(new WidgetItem(false, WidgetItem.TYPE_GIF, R.drawable.jinli, CommUtils.getString(com.hktf.uilib.R.string.widget_text_input)));

        // 创建自定义的 ItemDecoration
//        int spacingInPixels = UIUtils.dip2px(8);
//        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, spacingInPixels, true));

        widgetItemAdapter = new WidgetItemAdapter(list);
        recyclerView.setAdapter(widgetItemAdapter);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                return widgetItemAdapter.getItem(position).getWeight();
            }
        });
        setContentView(rootView);
        setOutsideTouchable(true);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        widgetItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                WidgetItem widgetItem = widgetItemAdapter.getItem(position);
                int itemType = widgetItem.getItemType();
                switch (itemType) {
                    case WidgetItem.TYPE_TEXT1:
                        showDlg(widgetItem, position);
                        return;
                    case WidgetItem.TYPE_TEXT2:
                        showDlg(widgetItem, position);
                        return;
                    case WidgetItem.TYPE_TEXT_CIRCLE:
                        showDlg(widgetItem, position);
                        return;
                    case WidgetItem.TYPE_JITANG:
                        WidgetItem newData = new WidgetItem();
                        WidgetItem.clone(widgetItem, newData);
                        notifyPick(newData, position);
                        return;
                }


                notifyPick(widgetItem, position);
            }
        });

        setClippingEnabled(false);

    }

    private void notifyPick(WidgetItem widgetItem, int position) {
        if (myListener != null) {
            myListener.onPick(widgetItem, position);
        }
        dismiss();
    }

    public void showDlg(WidgetItem widgetItem, int position) {
        int itemType = widgetItem.getItemType();
        Integer limit = null;
        String tip=UIUtils.getString(com.hktf.uilib.R.string.widget_text_input_tip);
        switch (itemType) {
            case WidgetItem.TYPE_TEXT1:
                break;
            case WidgetItem.TYPE_TEXT2:
                break;
            case WidgetItem.TYPE_TEXT_CIRCLE:
                limit = 1;
                tip=UIUtils.getString(com.hktf.uilib.R.string.widget_text_input_tip_one_text);
                break;
        }
        //新建标签
        DlgUtils.showEditDialog(getContentView().getContext(), "", "",
                tip,limit, new DlgUtils.RenameFileCallback() {

                    @Override
                    public void onConfirm(String name) {

                        WidgetItem newData = new WidgetItem();
                        WidgetItem.clone(widgetItem, newData);
                        newData.setTitle(name);
                        notifyPick(newData, position);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (myListener != null) {
            myListener.onDismiss();
        }
    }


    private MyListener myListener;

    public MyListener getMyListener() {
        return myListener;
    }

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }

    public static interface MyListener {
        void onPick(WidgetItem widgetItem, int position);

        void onDismiss();

    }

    private OnCheckChangeListener onCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public static interface OnCheckChangeListener {
        void onCheckChange(KeyValueItem keyValueItem, int position);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }
}
